<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
<script>
    // 定义目标
    class Object {
        constructor () {
            this.observers = []
        }
        // 添加观察者
        addServer (observer) {
            this.observers.push(observer)
        }
        notify () {
            this.observers.forEach(item => item.info())
        }
    }
    // 定义观察者
    class Observer {
        constructor (name) {
            this.name = name
        }
        info () {
            console.log(this.name + '收到消息')
        }
    }

    const obj = new Object()
    const observer1 = new Observer('观察者1')
    const observer2 = new Observer('观察者2')
    const observer3 = new Observer('观察者3')
    obj.addServer(observer1)
    obj.addServer(observer2)
    obj.addServer(observer3)
    obj.notify()
</script>
</html>